//
//  YGProductCenterViewController.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/3/10.
//


#import "YGProductCenterViewController.h"
#import "YGProductBLL.h"
#import "YGProductHeaderView.h"
#import "YGItemsCollectionViewCell.h"
#import "YGSelectView.h"
#import "YGGoodsSelectModel.h"
#import <MJRefresh/MJRefreshBackNormalFooter.h>
#import "YGProductCategoryView.h"
@interface YGProductCenterViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,YGProductHeaderViewDelegate,YGSelectViewDelegate,YGProductCategoryViewDelegate>
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)YGProductCenterModel *model;
@property (nonatomic, strong)YGGoodsTypeLowerModel *goodsModel;
@property (nonatomic, strong)YGProductHeaderView *headerView;
@property (nonatomic, strong) NSMutableArray *itemsArray;
@property (nonatomic, strong) NSArray <YGGoodsSelectModel *> *keshiArray;
@property (nonatomic, strong) YGSelectView *keshiSelectView;
@property (nonatomic, strong) YGSelectView *priceSelectView;
@property (nonatomic, strong) YGSelectView *sortSelectView;
@property (nonatomic, strong) YGGoodsSelectModel *currentSelectModel;
@property (nonatomic, strong) YGProductCategoryView  *categortView;
@property (nonatomic, strong) NSArray  *categoryArray;

@property (nonatomic, assign) NSInteger  page;
@property (nonatomic, assign) NSInteger  totalPage;



@end

@implementation YGProductCenterViewController

static NSString *identify = @"YGItemsCollectionViewCell";


#pragma private
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"产品中心";
    [self requestData];
    [self initWith];
    [self.collectionView.mj_footer endRefreshingWithCompletionBlock:^{
        
    }];
    // Do any additional setup after loading the view.
}

//网络接口
-(void)requestData{
    WeakSelf(self);
    [YGProductBLL requestProductWithTypeId:self.currentSelectModel.id
                              departmentId:self.currentSelectModel.departmentId
                                      sort:self.currentSelectModel.sortTag
                                      page:self.page
                                      rows:30
                                 goodsName:self.currentSelectModel.name
                                  minPrice:self.currentSelectModel.minValue
                                  maxPrice:self.currentSelectModel.maxValue
                                   Success:^(NSArray<YGProductItemModel *> * _Nonnull model,NSInteger totalPage) {
        weakself.totalPage = totalPage;
        [weakself.itemsArray addObjectsFromArray:model];
        [weakself.collectionView reloadData];
        [weakself footerStatus];
    }
                                      fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)requestKeshi{
    WeakSelf(self);
    [YGProductBLL requestKeshiWithSuccess:^(NSArray<YGGoodsSelectModel *> * _Nonnull model) {
        weakself.keshiArray = model;
        [weakself.keshiSelectView configureData:model];
        [weakself.keshiSelectView show];
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)requestCategory{
    WeakSelf(self);
    [YGProductBLL requestProductTypeWithSuccess:^(NSArray<YGGoodsSelectModel *> * _Nonnull model) {
        weakself.categoryArray = model;
        [weakself.categortView configureData:model];
        [weakself.categortView show];
    } fail:^(NSError * _Nonnull error) {
        
    }];

}
-(void)initWith{
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.collectionView];
    self.collectionView.top = self.headerView.bottom;
    [self.headerView configureData:[self headerViewSources]];
}

- (void)loadMoreAction{
    if (self.page >= self.totalPage) {
        return;
    }
    self.page += 1;
    [self requestData];
}

- (void)footerStatus{
    [self.collectionView.mj_footer endRefreshing];
    if (self.page >=  self.totalPage) {
        [self.collectionView.mj_footer endRefreshingWithNoMoreData];
    }
}

#pragma mark - seleview delegate
- (void)headerViewClickAction:(NSInteger)index{
    if (index == 0) {
        [self requestKeshi];
    }else if (index == 1) {
        [self requestCategory];
    }else if (index == 2){
        [self.priceSelectView configureData:[self priceData]];
        [self.priceSelectView show];
    }else{
        [self.sortSelectView configureData:[self sortData]];
        [self.sortSelectView show];
    }
}

#pragma mark - collection


//设置collecview每组所包含的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.itemsArray.count;
}

//点击collectionview触发事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}


//设置collectioncell的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
        
    YGItemsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    YGProductItemModel *model = [self.itemsArray objectAtIndex:indexPath.row];
    [cell configureData:model];

        return cell;
}

#pragma mark - YGSelectViewDelegate

- (void)selectViewWithIndex:(NSInteger)index selectView:(YGSelectView *)selectView{
    NSMutableArray *array = [NSMutableArray arrayWithArray:[self.headerView getHeaderTitles]];
    NSInteger tag = 0;
    NSString *title = nil;
    if (selectView == self.keshiSelectView) {
        YGGoodsSelectModel *model = [self.keshiArray objectAtIndex:index];
        title = model.name;
        tag = 0;
        self.currentSelectModel.departmentId = model.id;
    }else if(selectView == self.priceSelectView){
        YGGoodsSelectModel *model = [[self priceData] objectAtIndex:index];
        title = model.name;
        tag = 2;
        self.currentSelectModel.minValue = model.minValue;
        self.currentSelectModel.maxValue = model.maxValue;
    }else if (selectView == self.sortSelectView){
        tag = 3;
        YGGoodsSelectModel *model = [[self sortData] objectAtIndex:index];
        self.currentSelectModel.sortTag = model.sortTag;
        title = model.name;
    }else if (selectView == self.categortView){
        tag = 1;
        YGGoodsSelectModel *model = [[self.categortView getSecondArray] objectAtIndex:index];
        self.currentSelectModel.departmentId = model.departmentId;
        title = model.name;
    }
    
    [array replaceObjectAtIndex:tag withObject:title];
    [self.headerView reloadData:array];

    [self.collectionView.mj_footer resetNoMoreData];    //重新选择，重新设置下拉加载更多状态
    [self.itemsArray removeAllObjects]; //重新选择，全部删除旧数据
    [self requestData];
}

- (void)categoryViewWithIndex:(NSString *)parentId selectView:(YGProductCategoryView *)selectView{
    self.currentSelectModel.departmentId = parentId;
    
}
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        CGFloat width = (self.view.width - 30) /2.0;
        UICollectionViewFlowLayout *flowlayout=[[UICollectionViewFlowLayout alloc]init];
        flowlayout.itemSize = CGSizeMake(width, width + 60);
        [flowlayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, self.headerView.bottom,self.view.width ,self.view.height - self.headerView.bottom - kNavigationHeight) collectionViewLayout:flowlayout];
        flowlayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
//        _collectionView
        _collectionView.dataSource=self;
        _collectionView.delegate=self;
        _collectionView.backgroundColor = UIColorFromRGB(0xf5f5f5);
        [_collectionView registerClass:YGItemsCollectionViewCell.class forCellWithReuseIdentifier:identify];
        _collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreAction)];
        [_collectionView.mj_footer endRefreshingWithCompletionBlock:^{
            
        }];
    }
    return _collectionView;
}

- (YGProductHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[YGProductHeaderView alloc] initWithFrame:CGRectMake(0, kNavigationHeight, self.view.width, 50)];
        _headerView.delegate = self;
    }
    return _headerView;
}

- (YGSelectView *)keshiSelectView{
    if (!_keshiSelectView) {
        _keshiSelectView = [[YGSelectView alloc] initWithFrame:self.view.bounds];
        _keshiSelectView.top = self.headerView.bottom;
        _keshiSelectView.height = self.view.height - self.headerView.height;
        _keshiSelectView.delegate = self;
        _keshiSelectView.hidden = YES;
        [self.view addSubview:_keshiSelectView];
        _keshiSelectView.top = self.headerView.bottom;
    }
    return _keshiSelectView;
}

- (YGProductCategoryView *)categortView{
    if (!_categortView) {
        _categortView = [[YGProductCategoryView alloc] initWithFrame:self.view.bounds];
        _categortView.top = self.headerView.bottom;
        _categortView.height = self.view.height - self.headerView.height;
        _categortView.delegate = self;
        _categortView.hidden = YES;
        [self.view addSubview:_categortView];
        _categortView.top = self.headerView.bottom;
    }
    return _categortView;
}

- (YGSelectView *)priceSelectView{
    if (!_priceSelectView) {
        _priceSelectView = [[YGSelectView alloc] initWithFrame:self.view.bounds];
        _priceSelectView.top = self.headerView.bottom;
        _priceSelectView.height = self.view.height - self.headerView.height;
        _priceSelectView.delegate = self;
        _priceSelectView.hidden = YES;
        [self.view addSubview:_priceSelectView];
        _priceSelectView.top = self.headerView.bottom;
    }
    return _priceSelectView;
}

- (YGSelectView *)sortSelectView{
    if (!_sortSelectView) {
        _sortSelectView = [[YGSelectView alloc] initWithFrame:self.view.bounds];
        _sortSelectView.top = self.headerView.bottom;
        _sortSelectView.height = self.view.height - self.headerView.height;
        _sortSelectView.delegate = self;
        _sortSelectView.hidden = YES;
        [self.view addSubview:_sortSelectView];
        _sortSelectView.top = self.headerView.bottom;
    }
    return _sortSelectView;
}

- (YGGoodsSelectModel *)currentSelectModel{
    if (!_currentSelectModel) {
        _currentSelectModel = [[YGGoodsSelectModel alloc] init];
    }
    return _currentSelectModel;
}

- (NSMutableArray *)itemsArray{
    if (!_itemsArray) {
        _itemsArray = [NSMutableArray array];
    }
    return _itemsArray;
}

- (NSArray *)headerViewSources{
    return @[@"科室",@"分类",@"价格",@"排序"];
}

- (NSArray *)priceData{
    NSMutableArray *array = [NSMutableArray array];
    for (NSDictionary *dic in [self priceSource]) {
        YGGoodsSelectModel *model = [[YGGoodsSelectModel alloc] init];
        model.name = [dic objectForKey:@"title"];
        model.maxValue = [[dic objectForKey:@"maxValue"] floatValue];
        model.minValue = [[dic objectForKey:@"value"] floatValue];
        [array addObject:model];
    }
    return array;
}

- (NSArray *)priceSource{
    return @[@{@"value":@"0",@"maxValue":@"0",@"title":@"无限"},
             @{@"value":@"1",@"maxValue":@"99",@"title":@"1-99元"},
             @{@"value":@"100",@"maxValue":@"999",@"title":@"100-999元"},
             @{@"value":@"1000",@"maxValue":@"10000000",@"title":@"1000元以上"},
             ];
}

- (NSArray *)sortData{
    NSMutableArray *array = [NSMutableArray array];
    NSInteger tag = 1;
    for (NSDictionary *dic in [self sortSource]) {
        YGGoodsSelectModel *model = [[YGGoodsSelectModel alloc] init];
        model.name = [dic objectForKey:@"title"];
        model.sortTag = tag;
        tag += 1;
        [array addObject:model];
    }
    return array;
}

- (NSArray *)sortSource{
    return @[@{@"title":@"最新"},
             @{@"title":@"销售"},
             @{@"title":@"价格从高到低"},
             @{@"title":@"价格从低到高"},
             ];
}
- (NSInteger)page{
    if (_page == 0) {
        return 1;
    }
    return _page;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
